﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTO;
//using System.Linq;
using System.Data;


namespace DAO
{
    public class DanhMucChinhDAO
    {
        public static List<DanhMucChinhDTO> layDSTenDanhMucChinh()
        {
            List<DanhMucChinhDTO> result = new List<DanhMucChinhDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = (from dsDanhMuc in dbRaoVat.DANHMUCCHINHs.Where(dm => dm.Deleted.Equals(0))
                         select new
                         {
                             dsDanhMuc.MaDanhMucChinh,
                             dsDanhMuc.TenDanhMucChinh,
                             dsDanhMuc.ThuTu,
                         }).ToList();

            foreach (var ds in query)
            {
                result.Add(new DanhMucChinhDTO(ds.MaDanhMucChinh, ds.TenDanhMucChinh, (int)ds.ThuTu));
            }

            return result;
        }

        public static List<DanhMucChinhDTO> getAll()
        {
            List<DanhMucChinhDTO> result = new List<DanhMucChinhDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DANHMUCCHINHs.Where(dm => dm.Deleted.Equals(0)
                ).OrderBy(dm => dm.ThuTu).ToList();

            foreach (var ds in query)
            {
                DanhMucChinhDTO dto = new DanhMucChinhDTO();

                dto.MaDanhMucChinh = ds.MaDanhMucChinh;
                dto.TenDanhMucChinh = ds.TenDanhMucChinh;
                dto.ThuTu = (int)ds.ThuTu;
                dto.Deleted = (int)ds.Deleted;
                result.Add(dto);
            }
            return result;
        }

        public static DanhMucChinhDTO getAlongCode(int maDanhMucChinh)
        {
            DanhMucChinhDTO result = new DanhMucChinhDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DANHMUCCHINHs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaDanhMucChinh == maDanhMucChinh).Single();

            result.MaDanhMucChinh = query.MaDanhMucChinh;
            result.TenDanhMucChinh = query.TenDanhMucChinh;

            return result;
        }

        public static void Insert(DanhMucChinhDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DANHMUCCHINH danhmucchinh = new DANHMUCCHINH();
            danhmucchinh.TenDanhMucChinh = _insert.TenDanhMucChinh;
            danhmucchinh.Deleted = _insert.Deleted;
            danhmucchinh.ThuTu = _insert.ThuTu;

            dbRaoVat.DANHMUCCHINHs.InsertOnSubmit(danhmucchinh);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maDanhMucChinh)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DANHMUCCHINH _delete = (from n in dbRaoVat.DANHMUCCHINHs
                                 where n.MaDanhMucChinh == maDanhMucChinh && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(DanhMucChinhDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DANHMUCCHINH _update = (from n in dbRaoVat.DANHMUCCHINHs
                                    where n.MaDanhMucChinh == dto.MaDanhMucChinh && n.Deleted == 0
                                    select n).Single();

            _update.TenDanhMucChinh = dto.TenDanhMucChinh;            

            dbRaoVat.SubmitChanges();
        }

        public static int getMa(string tenDanhMuc)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DANHMUCCHINHs.Where(dm =>
                dm.TenDanhMucChinh == tenDanhMuc).First();

            return query.MaDanhMucChinh;
        }

        public static string getTen(int maDanhMuc)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DANHMUCCHINHs.Where(dm =>
                dm.MaDanhMucChinh == maDanhMuc).First();

            return query.TenDanhMucChinh;
        }
    }
}
